package com.se2.antiqueexhibition.Controller;

import com.se2.antiqueexhibition.Service.CommentService;
import com.se2.antiqueexhibition.pojo.Comment;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    /**
     * 添加评论
     */
    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> addComment(@RequestBody Comment comment) {
        Map<String, Object> result = new HashMap<>();
        if (commentService.addComment(comment)) {
            result.put("success", true);
            result.put("message", "评论已提交，等待审核");
        } else {
            result.put("success", false);
            result.put("message", "评论提交失败");
        }
        return result;
    }

    /**
     * 根据目标获取评论列表（已审核通过）
     */
    @GetMapping("/list/{targetType}/{targetId}")
    @ResponseBody
    public Map<String, Object> getCommentsByTarget(@PathVariable String targetType, 
                                                    @PathVariable Integer targetId) {
        Map<String, Object> result = new HashMap<>();
        List<Comment> comments = commentService.getCommentsByTarget(targetType, targetId);
        result.put("success", true);
        result.put("data", comments);
        return result;
    }

    /**
     * 根据用户ID获取评论列表
     */
    @GetMapping("/user/{userId}")
    @ResponseBody
    public Map<String, Object> getCommentsByUserId(@PathVariable Integer userId) {
        Map<String, Object> result = new HashMap<>();
        List<Comment> comments = commentService.getCommentsByUserId(userId);
        result.put("success", true);
        result.put("data", comments);
        return result;
    }

    /**
     * 获取所有评论（管理端）
     */
    @GetMapping("/all")
    public String getAllComments(ModelMap model) {
        List<Comment> comments = commentService.getAllComments();
        model.put("comments", comments);
        return "admin/comment/list";
    }

    /**
     * 获取待审核评论（管理端）
     */
    @GetMapping("/pending")
    public String getPendingComments(ModelMap model) {
        List<Comment> comments = commentService.getCommentsByStatus(0);
        model.put("comments", comments);
        return "admin/comment/pending";
    }

    /**
     * 获取待审核评论（API）
     */
    @GetMapping("/api/pending")
    @ResponseBody
    public Map<String, Object> getPendingCommentsApi() {
        Map<String, Object> result = new HashMap<>();
        List<Comment> comments = commentService.getCommentsByStatus(0);
        result.put("success", true);
        result.put("data", comments);
        return result;
    }

    /**
     * 审核通过评论
     */
    @PostMapping("/approve/{id}")
    @ResponseBody
    public Map<String, Object> approveComment(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        if (commentService.approveComment(id)) {
            result.put("success", true);
            result.put("message", "审核通过");
        } else {
            result.put("success", false);
            result.put("message", "操作失败");
        }
        return result;
    }

    /**
     * 拒绝评论
     */
    @PostMapping("/reject/{id}")
    @ResponseBody
    public Map<String, Object> rejectComment(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        if (commentService.rejectComment(id)) {
            result.put("success", true);
            result.put("message", "已拒绝");
        } else {
            result.put("success", false);
            result.put("message", "操作失败");
        }
        return result;
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> deleteComment(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        if (commentService.deleteComment(id)) {
            result.put("success", true);
            result.put("message", "删除成功");
        } else {
            result.put("success", false);
            result.put("message", "删除失败");
        }
        return result;
    }

    /**
     * 更新评论内容
     */
    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> updateComment(@RequestBody Comment comment) {
        Map<String, Object> result = new HashMap<>();
        if (commentService.updateComment(comment)) {
            result.put("success", true);
            result.put("message", "更新成功");
        } else {
            result.put("success", false);
            result.put("message", "更新失败");
        }
        return result;
    }
}
